﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Conexion;
using AccesoDatos;
using System.Runtime.InteropServices;

namespace RMA.Pantallas
{
    public partial class frmCatProveedores : Form
    {
        [DllImport("user32.dll")]
        static extern bool AnimateWindow(IntPtr hwnd, uint dwTime, uint dwFlags);

        public class WinApi
        {
            public static int GWL_EXSTYLE = -20;
            public static int WS_EX_LAYERED = 0x80000;
            public static int LWA_ALPHA = 0x2;
            [DllImport("user32")]
            public static extern int SetLayeredWindowAttributes(IntPtr hWnd, byte crey, byte alpha, int dwFlags);
            [DllImport("user32")]
            public static extern int GetLayeredWindowAttributes(IntPtr hWnd, byte crey, out byte alpha, int dwFlags);
            [DllImport("user32")]
            public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
            [DllImport("user32")]
            public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        }
        void mkwndtrans(IntPtr hWnd, int num)
        {
            int oldWinStyle = WinApi.GetWindowLong(hWnd, WinApi.GWL_EXSTYLE);
            WinApi.SetWindowLong(hWnd, WinApi.GWL_EXSTYLE,
            num < 255 ? oldWinStyle | WinApi.WS_EX_LAYERED :
            oldWinStyle & WinApi.WS_EX_LAYERED);
            WinApi.SetLayeredWindowAttributes(hWnd, 0, (byte)num, WinApi.LWA_ALPHA);
        }
        enum AnimateWindowFlags : uint
        {
            AW_HOR_POSITIVE = 0x00000007,
            AW_HOR_NEGATIVE = 0x00000002,
            AW_VER_POSITIVE = 0x00000004,
            AW_VER_NEGATIVE = 0x00000008,
            AW_CENTER = 0x00000010,
            AW_HIDE = 0x00010000,
            AW_ACTIVATE = 0x00020000,
            AW_SLIDE = 0x00040000,
            AW_BLEND = 0x00080000
        }

        public frmCatProveedores()
        {
            InitializeComponent();
        }

        private void frmCatProveedores_Load(object sender, EventArgs e)
        {
            CargarProveedores();
        }

        public void CargarProveedores()
        {
            dgProveedores.DataSource = null;
            Manejador_proveedores objProv = new Manejador_proveedores();
            dgProveedores.AutoGenerateColumns = false;
            dgProveedores.DataSource = objProv.RegresaProveedores();
            foreach (DataGridViewColumn ca in dgProveedores.Columns)
            {
                ca.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            }
        
        }

        private void dgProveedores_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string strId = dgProveedores.Rows[e.RowIndex].Cells[0].Value.ToString().Trim();
            lblId.Text = strId;

            
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            bool Modificar = false;
            Manejador_proveedores objProv = new Manejador_proveedores();
            clsConexion objConn = new clsConexion();

            try
            {
                if (lblId.Text != "")
                {
                    if (MessageBox.Show("Desea Eliminar del Listado el Proveedor seleccionado?", "Aviso", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        Modificar = objProv.CambiaStatusProveedor(lblId.Text, "I");
                        MessageBox.Show("Registro Eliminado", "Exito", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        CargarProveedores();
                    }
                    else
                    {
                        MessageBox.Show("Operacion Cancelada", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    MessageBox.Show("Debe seleccionar un regristro para poder eliminarlo", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                
                

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }



        }

        private void dgProveedores_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            clsProveedores objProv = new clsProveedores();

            objProv.IdProv = int.Parse(dgProveedores.Rows[e.RowIndex].Cells[0].Value.ToString());
            objProv.NombreProv = dgProveedores.Rows[e.RowIndex].Cells[1].Value.ToString();
            objProv.Contacto = dgProveedores.Rows[e.RowIndex].Cells[2].Value.ToString();
            objProv.Email = dgProveedores.Rows[e.RowIndex].Cells[3].Value.ToString();
            objProv.Telefono = dgProveedores.Rows[e.RowIndex].Cells[4].Value.ToString();
            objProv.PlazoPago = dgProveedores.Rows[e.RowIndex].Cells[5].Value.ToString();

            frmModProv frmMP = new frmModProv(objProv);
            AnimateWindow(frmMP.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            frmMP.ShowDialog();

            CargarProveedores();

            

            
        }

        private void btnAgregar_Click(object sender, EventArgs e)
        {
            frmAgregaProveedor frmAP = new frmAgregaProveedor();
            AnimateWindow(frmAP.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            frmAP.ShowDialog();

            CargarProveedores();
        }
    }
}
